﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using QLVMBAYDatacess;
using QLVMBAYDomain;

namespace QLVMBAY.Admin
{
    public partial class QLLichBay : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
            }
        }
        private void LoadGrid()
        {
            grdLichBay.DataSource = ChuyenBayDA.Inst.GetAll(true);
            grdLichBay.DataBind();
            if (grdLichBay.PageCount <= 1)
            {
                grdLichBay.PagerStyle.Visible = false;
            }
        }
        void Load_dlTuyenBay()
        {
            List<TuyenBay> list = new List<TuyenBay>();
            list = TuyenBayDA.Inst.GetAll(true);
            dlTuyenBay.DataSource = list;
            dlTuyenBay.DataTextField = "TenTuyenBay";
            dlTuyenBay.DataValueField = "MaTuyenBay";
            dlTuyenBay.DataBind();
            dlTuyenBay.Items.Insert(0, new ListItem("Chọn", "0"));
        }
        void Load_dlHangBay()
        {
            List<HangHangKhong> list = new List<HangHangKhong>();
            list = HangHangKhongDA.Inst.GetAll(true);
            dlHangBay.DataSource = list;
            dlHangBay.DataTextField = "TenHang";
            dlHangBay.DataValueField = "MaHang";
            dlHangBay.DataBind();
            dlHangBay.Items.Insert(0, new ListItem("Chọn", "0"));
        }
        void Load_dlGio()
        {

        }
        private void ResetControlValues()
        {
            txtMaCB.Text = "";
            txtMaCB.ReadOnly = false;
            txtTenCB.Text = "";            
            txtNgayBay.Text = "";
            txtSLHK.Text = "";            
        }
        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            pnView.Visible = false;
            Insert = true;
            ResetControlValues();
            Load_dlTuyenBay();
            Load_dlHangBay();
            ClientComm.Ints.LoadGio(dllGioBay1, false);
            ClientComm.Ints.LoadPhut(dllGioBay2, false);
            ClientComm.Ints.LoadGio(dllTGBay1, false);
            ClientComm.Ints.LoadPhut(dllTGBay2, false);
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                ChuyenBay CB = new ChuyenBay();
                CB.MaChuyenBay = txtMaCB.Text;
                CB.TenChuyenBay = txtTenCB.Text;
                CB.ItemTuyenBay = new TuyenBay() {MaTuyenBay=dlTuyenBay.SelectedValue };
                CB.ItemHangHangKhong = new HangHangKhong() { MaHang = dlHangBay.SelectedValue };
                CB.NgayBay = ClientComm.Ints.ToDate(txtNgayBay.Text);
                CB.TgKhoiHanh = Convert.ToDateTime(dllGioBay1.Text + ":"+dllGioBay2.Text).TimeOfDay;
                CB.ThoiGianBay = Convert.ToDateTime(dllTGBay1.Text + ":" + dllTGBay2.Text).TimeOfDay;                     
                CB.SLHanhKhach  = int.Parse(txtSLHK.Text);                   
                if (Insert == true)
                {
                    ChuyenBayDA.Inst.Insert(CB);
                }
                else
                {
                    ChuyenBayDA.Inst.Update(CB);
                }
                LoadGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdLichBay_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var itemCB = ChuyenBayDA.Inst.GetByPrimaryKey(false, e.Item.Cells[0].Text);
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;
                    txtMaCB.ReadOnly = true;
                    txtMaCB.Text = itemCB.MaChuyenBay.ToString();
                    txtTenCB.Text = itemCB.TenChuyenBay.ToString();
                    Load_dlTuyenBay();
                    dlTuyenBay.SelectedValue = itemCB.ItemTuyenBay.MaTuyenBay;
                    Load_dlHangBay();
                    dlHangBay.SelectedValue = itemCB.ItemHangHangKhong.MaHang;
                    txtNgayBay.Text = itemCB.NgayBay.ToString("dd/MM/yyyy");
                    ClientComm.Ints.LoadGio(dllGioBay1, false);           
                    dllGioBay1.Text = itemCB.TgKhoiHanh.Hours.ToString();
                    ClientComm.Ints.LoadPhut(dllGioBay2, false);
                    dllGioBay2.Text = itemCB.TgKhoiHanh.Minutes.ToString();
                    ClientComm.Ints.LoadGio(dllTGBay1, false);
                    dllTGBay1.Text = itemCB.ThoiGianBay.Hours.ToString();
                    ClientComm.Ints.LoadPhut(dllTGBay2, false);
                    dllTGBay2.Text = itemCB.ThoiGianBay.Minutes.ToString();                                       
                    txtSLHK.Text = itemCB.SLHanhKhach.ToString();
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;

                case "Delete":
                    ChuyenBayDA.Inst.Delete(itemCB);
                    LoadGrid();
                    break;
            }
        }

        protected void grdLichBay_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                var CB = (ChuyenBay)e.Item.DataItem;
                var lbl1 = e.Item.FindControl("lblTenTB") as Label;
                lbl1.Text = CB.ItemTuyenBay.TenTuyenBay;
                var lbl2 = e.Item.FindControl("lblTenHang") as Label;                
                lbl2.Text = CB.ItemHangHangKhong.TenHang;
                var lbl3 = e.Item.FindControl("lblTgKhoiHanh") as Label;
                lbl3.Text = CB.TgKhoiHanh.ToString("hh':'mm");
                var lbl4 = e.Item.FindControl("lblTgBay") as Label;
                if (CB.ThoiGianBay.Days > 0)
                {
                    lbl4.Text = CB.ThoiGianBay.ToString("d'd hh'h'mm'p'");
                }
                else if (CB.ThoiGianBay.Hours > 0)
                {
                    lbl4.Text = CB.ThoiGianBay.ToString("hh'h'mm'p'");
                }
                else
                {
                    lbl4.Text = CB.ThoiGianBay.ToString("mm'p'");
                }
                var lbl5 = e.Item.FindControl("lblNgayBay") as Label;
                lbl5.Text = CB.NgayBay.ToString("dd/MM/yyyy");
            }
        }

        protected void grdLichBay_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdLichBay.CurrentPageIndex = e.NewPageIndex;
            LoadGrid();
        }
    }
}